package doyouknow.group.CnAndZhb.response;

import doyouknow.group.CnAndZhb.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by a10.13.6 on 2019/4/23.
 */

public class CommonReturnType {


    /**
     * 通用错误返回码 本类用于返回所有错误代码  当处理 成功之后会返回相应 的 json 结构体
     */


    //对应请求的处理结果，fail,success
    private String status;

    //若status=success，则data内返回前端所需要的json数据
    //若status=fail,则data返回通用的错误码格式
    private Object data;


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    public static CommonReturnType create(Object result){
        return create(result,"success");
    }

    public static CommonReturnType create(Object result, String status) {
        CommonReturnType type =new CommonReturnType();
        type.setStatus(status);
        type.setData(result);
        return  type;
    }

    /**
     *
     * 隐藏不需要的key
     *
     * @param object
     * @param Filed
     * @return
     */
    public static Map<String, Object> ObjFiledHide(Object object, ArrayList<String> Filed) {

        Map<String, Object> NewMap = new HashMap<>();

        try {
            Map<String, Object>  Map = ObjectUtils.objectToMap(object);
            Iterator<Map.Entry<String, Object>> entries = Map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, Object> entry = entries.next();

                if(Filed.contains(entry.getKey())){
                    NewMap.put(entry.getKey(),entry.getValue());
                }
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return NewMap;

    }

}
